﻿using System;
using System.Linq;

namespace ECommerce.Controls
{
	public partial class Login : BaseUserControl
	{
		protected void Page_PreRender(object sender, EventArgs e)
		{
			this.LLoginTitle.Text = GetLocalResourceString("Login.Title");
			this.LLoginTips.Text = GetLocalResourceString("Login.Tips");
		}

		protected void Login1_PreRender(object sender, EventArgs e)
		{ 
			if(!IsPostBack)
			{
				//this.Page.Title = GetLocalResourceString("Ecommerce.PageTitle.Login");

				Login1.PasswordRecoveryText = GetLocalResourceString("Login.PasswordRecoveryText");
			}
		}

		protected void UserNameRequired_PreRender(object sender, EventArgs e)
		{
			var UserNameRequired = sender as System.Web.UI.WebControls.RequiredFieldValidator;

			UserNameRequired.ErrorMessage = GetLocalResourceString("Login.UserNameRequired.ErrorMessage");
		}

		protected void PasswordRequired_PreRender(object sender, EventArgs e)
		{
			var PasswordRequired = sender as System.Web.UI.WebControls.RequiredFieldValidator;

			PasswordRequired.ErrorMessage = GetLocalResourceString("Login.PasswordRequired.ErrorMessage");
		}

		protected void RememberMe_PreRender(object sender, EventArgs e)
		{
			var RememberMe = sender as System.Web.UI.WebControls.CheckBox;

			RememberMe.Text = GetLocalResourceString("Login.RememberMe.Text");
		}

		protected void LoginButton_PreRender(object sender, EventArgs e)
		{
			var LoginButton = sender as System.Web.UI.WebControls.Button;

			LoginButton.Text = GetLocalResourceString("Login.LoginButton.Text");
		}

		protected void Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
		{
			e.Authenticated = System.Web.Security.Roles.GetRolesForUser(Login1.UserName).Contains("customer");
		}

		protected void Login1_LoggedIn(object sender, EventArgs e)
		{
			var user = System.Web.Security.Membership.GetUser(Login1.UserName);
			user.LastLoginDate = DateTime.Now;
			System.Web.Security.Membership.UpdateUser(user);

			Response.Redirect("~/Customer/MyAccountHome.aspx");
		}
	}
}